package com.j2some;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.http.entity.ContentType;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.RequestGongWuShu.CWRService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yishun.springboot.Application;
import com.yishun.springboot.dao.CWRServiceTest;
import com.yishun.springboot.mapper.GwsCWRMapper;
import com.yishun.springboot.pojo.UploadGWSAddTeam;
import com.yishun.springboot.service.InnerFormalGWSCWRService;

import lombok.extern.slf4j.Slf4j;

import com.yishun.springboot.pojo.GwsDictListJobName;
import com.yishun.springboot.pojo.GwsTeamType;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
/**
 * 接口分类--[Come in]是查询并且同步到本地的，[Out put]是上传到工务署的。√是正式环境测过完成可以进行封装了的。
 * 所有的实名制真实数据接口测试 完成√√的可直接对上自己方的web后台管理页面 有疑问或未完成的的打?
 * 
 * @author YS
 *
 */
public class GWSMybatisTestCWR {

	@Autowired
	GwsCWRMapper cWRMapper;

	@Autowired
	GwsCWRMapper gwsCWRMapper;

	@Autowired
	InnerFormalGWSCWRService innerFormalGWSCWRService;

	/**
	 * [Come in]√ 批量从从工务署获取工种类型的接口插入到本地mysql 理论上来说只需要请求一次 //truncate table
	 * dictlist_jobname。 mysql清空重置自增id（测试环境方便用） 这些只请求一次的就保存着的应该是要在web层请求的
	 * 
	 * @throws NoSuchAlgorithmException
	 */

	public void UpdateGWSCWRDictListJobName() throws NoSuchAlgorithmException {
		CWRService c = new CWRService();
		List<GwsDictListJobName> gwsDictListJobNameList = null;
		try {
			gwsDictListJobNameList = c.getDictListJobName();
		} catch (Exception e) {
			log.info("工务署端口--请求获取失败！");
		}
		int succCount = 0;// 插入本机成功数量

		cWRMapper.truncateDictListJobName();// 删除
		// 批量插入
		for (int i = 0; i < gwsDictListJobNameList.size(); i++) {
			GwsDictListJobName d = gwsDictListJobNameList.get(i);
			succCount += cWRMapper.innerOneDictListJobNameRecord(d);
		}
		log.info("总共完成插入：" + succCount + "条");
		// 查询全部
		List<GwsDictListJobName> l = cWRMapper.findAllDictListJobName();
	}

	/**
	 * [Come in]√ 批量从从工务署获取班组类型的接口插入到本地mysql √
	 * 
	 * @throws NoSuchAlgorithmException
	 */
	public void UpdateGWSCWRGetTeamType() throws NoSuchAlgorithmException {
		CWRService c = new CWRService();

		List<GwsTeamType> list = c.GetTeamType();
		int succCount = 0;

		cWRMapper.truncateTeamType();
		// 批量插入
		for (int i = 0; i < list.size(); i++) {
			GwsTeamType d = list.get(i);
			succCount += cWRMapper.innerOneTeamTypeRecord(d);
		}
		log.info("总共完成插入：" + succCount + "条");
		// 查询全部
		List<GwsTeamType> l = cWRMapper.findTeamTypeAll();

	}

	@Test
	/**
	 * 上传/新增 班组信息到工务署，?
	 * 疑惑的地方是工务署的测试ip白名单过了有效期，周六的今天又联系不上工务署就没法测，只能尽量模拟数据一样的，模拟完了但是不知道能不能通过工务署所以是备注问号
	 * 以下一样
	 * 
	 * @throws NoSuchAlgorithmException
	 */
	public void UpdateGWSCWRAddTeamType() throws NoSuchAlgorithmException {

//		File pdfFile = new File("C://Users//Administrator//Desktop//1.png");
//		MultipartFile multipartFile = null;
//		try {
//			FileInputStream fileInputStream = new FileInputStream(pdfFile);
//			multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(),
//					ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		log.info(innerFormalGWSCWRService.UploadImage(multipartFile));

//		log.info(innerFormalGWSCWRService.UploadImage("D://uploadGwsStoreYzwImg//yzwworkerlist//headImage//302345//375de8ce-fc27-4d71-9d96-61473ec4cbb0.png"));
		log.info(innerFormalGWSCWRService.UploadImage("6e16c5dd1ee747f19434a8c29b8fee03","C://Users//Administrator//Desktop//2.jpeg"));
		log.info(innerFormalGWSCWRService.UploadImage("8c023a75eb8b4ae6bf5efce7fbe36af4","C://Users//Administrator//Desktop//2.jpeg"));

//		log.info(gwsCWRMapper.innerOneTeamRecord("123", "123", "123", "123", "123"));

//		List<GwsTeamType> l = cWRMapper.findTeamTypeAll();
//		log.info(l.size());
//        String info = "";
//		CWRService c = new CWRService();
//
//		UploadGWSAddTeam d = new UploadGWSAddTeam();
//		d.setEmp_company("广东省第一建筑工程有限公司");
//		d.setProject_ID("GDSYEQ-010-2019");
//		d.setSUID("914400001903237000");
//		d.setTeam_name("渣渣輝");
//		d.setTeam_type_name("混凝土班"); // 之后要改成前台传过来的teamType value
//		d.setTeam_type_order("CONCRETE"); // 之后要改成前台传过来的teamType order
//
//		info = c.addTeam(d);
//		log.info("end"+info);

	}

	/**
	 * image地址获取 ? 1218 开始这里做上传图片接口
	 */
//	public void TestImageAddressGet() {
//		CWRServiceDemo c = new CWRServiceDemo();
//
//		c.UploadImage();
//
//	}

	// 打杂方法]
	public void TestLittle() {
		UploadGWSAddTeam d = new UploadGWSAddTeam();
		d.setEmp_company("广东省第一建筑工程有限公司");
		d.setProject_ID("GDSYEQ-010-2019");
		d.setSUID(null);
		d.setTeam_name("渣渣輝");
		d.setTeam_type_name("混凝土班");
		d.setTeam_type_order("CONCRETE");
		String f = "";
		String s = "";
		f = JSONObject.toJSONString(d, SerializerFeature.QuoteFieldNames, SerializerFeature.WriteNullStringAsEmpty);
		f = f.replaceAll("\\\"", "");
		s = JSON.toJSONString(d, SerializerFeature.QuoteFieldNames);
		log.info("f:" + f);
		log.info("s:" + s);

	}

}
